package me.ziry.ui;

import java.util.ArrayList;
import java.util.List;

/* 机试题20210928 */
public class Test {

    static class User {
        public static final int Fizz = 3;
        public static final int Buzz = 5;


        public User(int id) {
            this.id = id;
        }

        int id;


        int sayId;

        String say;

        public boolean isSay() {
            return say != null;
        }

        public User say(int i) {
            sayId = i;
            String s = null;

            if (User.checkFizzBuzz(sayId)) {
                s = "FizzBuzz";
            } else if (User.checkBuzz(sayId)) {
                s = "Buzz";
            } else if (User.checkFizz(sayId)) {
                s = "Fizz";
            }
            if (s != null) {
                say = s;
            }
            System.out.println(this);
            return this;
        }

        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", sayId=" + sayId +
                    ", say='" + say + '\'' +
                    '}';
        }

        public static List<User> buildUser(int size) {
            List<User> user = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                user.add(new User(i));
            }
            return user;
        }

        public static boolean checkFizzBuzz(int i) {
            return checkFizz(i) && checkBuzz(i);
        }


        public static boolean checkFizz(int i) {
            return i % Fizz == 0 || (i + "").contains("" + Fizz);
        }

        public static boolean checkBuzz(int i) {
            return i % Buzz == 0 || (i + "").contains("" + Buzz);
        }

    }


    public static void main(String[] args) throws Exception {

        List<User> users = User.buildUser(100);

        List<User> sayUsers = new ArrayList<>();
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i).say(i);
            if (user.isSay()) {
                sayUsers.add(user);
            }
        }

        for (int i = 0; i < sayUsers.size(); i++) {
            User user = sayUsers.get(i);
            if (user.isSay()) {
                String s = null;
                if (User.checkFizzBuzz(user.sayId)) {
                    s = "FizzBuzz";
                } else if (User.checkBuzz(user.sayId)) {
                    s = "Buzz";
                } else if (User.checkFizz(user.sayId)) {
                    s = "Fizz";
                }
                if (s == null) {
                    throw new Exception("nullSay");
                }
                if (!s.equalsIgnoreCase(user.say)) {
                    throw new Exception("errSay");
                }
                System.out.println(user);
            } else {
                throw new Exception("noSay");
            }
        }


    }
}
